﻿using System;
using System.Reflection;
using System.Configuration;
namespace Things.DALFactory
{
    /// <summary>
    /// Abstract Factory pattern to create the DAL¡£
    /// Èç¹ûÔÚÕâÀï´´½¨¶ÔÏó±¨´í£¬Çë¼ì²éweb.configÀïÊÇ·ñÐÞ¸ÄÁË<add key="DAL" value="Things.SQLServerDAL" />¡£
    /// </summary>
    public sealed class DataAccess
    {
        private static readonly string AssemblyPath = ConfigurationManager.AppSettings["DAL"];
        public DataAccess()
        { }

        #region CreateObject

        //²»Ê¹ÓÃ»º´æ
        private static object CreateObjectNoCache(string AssemblyPath, string classNamespace)
        {
            try
            {
                object objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);
                return objType;
            }
            catch//(System.Exception ex)
            {
                //string str=ex.Message;// ¼ÇÂ¼´íÎóÈÕÖ¾
                return null;
            }

        }
        //Ê¹ÓÃ»º´æ
        private static object CreateObject(string AssemblyPath, string classNamespace)
        {
            object objType = DataCache.GetCache(classNamespace);
            if (objType == null)
            {
                try
                {
                    objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);
                    DataCache.SetCache(classNamespace, objType);// Ð´Èë»º´æ
                }
                catch//(System.Exception ex)
                {
                    //string str=ex.Message;// ¼ÇÂ¼´íÎóÈÕÖ¾
                }
            }
            return objType;
        }
        #endregion

        #region ·ºÐÍÉú³É
        ///// <summary>
        ///// ´´½¨Êý¾Ý²ã½Ó¿Ú¡£
        ///// </summary>
        //public static t Create(string ClassName)
        //{

        //    string ClassNamespace = AssemblyPath +"."+ ClassName;
        //    object objType = CreateObject(AssemblyPath, ClassNamespace);
        //    return (t)objType;
        //}
        #endregion

        #region CreateSysManage
        public static Things.IDAL.ISysManage CreateSysManage()
        {
            //·½Ê½1			
            //return (Things.IDAL.ISysManage)Assembly.Load(AssemblyPath).CreateInstance(AssemblyPath+".SysManage");

            //·½Ê½2 			
            string classNamespace = AssemblyPath + ".SysManage";
            object objType = CreateObject(AssemblyPath, classNamespace);
            return (Things.IDAL.ISysManage)objType;
        }
        #endregion


        /// <summary>
        /// 创建Buyer数据层接口。
        /// </summary>
        public static Things.IDAL.IBuyer CreateBuyer()
        {

            string ClassNamespace = AssemblyPath + ".Buyer";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Things.IDAL.IBuyer)objType;
        }


        /// <summary>
        /// 创建BuyerAddress数据层接口。
        /// </summary>
        public static Things.IDAL.IBuyerAddress CreateBuyerAddress()
        {

            string ClassNamespace = AssemblyPath + ".BuyerAddress";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Things.IDAL.IBuyerAddress)objType;
        }


        /// <summary>
        /// 创建BuyerInfo数据层接口。
        /// </summary>
        public static Things.IDAL.IBuyerInfo CreateBuyerInfo()
        {

            string ClassNamespace = AssemblyPath + ".BuyerInfo";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Things.IDAL.IBuyerInfo)objType;
        }

        /// <summary>
        /// 创建GoodsOrderPriceR数据层接口。
        /// </summary>
        public static Things.IDAL.IGoodsOrderPriceR CreateGoodsOrderPriceR()
        {

            string ClassNamespace = AssemblyPath + ".GoodsOrderPriceR";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Things.IDAL.IGoodsOrderPriceR)objType;
        }


        /// <summary>
        /// ??Crops??????
        /// </summary>
        public static Things.IDAL.ICrops CreateCrops()
        {

            string ClassNamespace = AssemblyPath + ".Crops";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Things.IDAL.ICrops)objType;
        }


        /// <summary>
        /// ??CropsCategory??????
        /// </summary>
        public static Things.IDAL.ICropsCategory CreateCropsCategory()
        {

            string ClassNamespace = AssemblyPath + ".CropsCategory";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Things.IDAL.ICropsCategory)objType;
        }


        /// <summary>
        /// ??CropsPlantProgress??????
        /// </summary>
        public static Things.IDAL.ICropsPlantProgress CreateCropsPlantProgress()
        {

            string ClassNamespace = AssemblyPath + ".CropsPlantProgress";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Things.IDAL.ICropsPlantProgress)objType;
        }


        /// <summary>
        /// ??CropsPlantProgressInfo??????
        /// </summary>
        public static Things.IDAL.ICropsPlantProgressInfo CreateCropsPlantProgressInfo()
        {

            string ClassNamespace = AssemblyPath + ".CropsPlantProgressInfo";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Things.IDAL.ICropsPlantProgressInfo)objType;
        }


        /// <summary>
        /// ??CropsProduct??????
        /// </summary>
        public static Things.IDAL.ICropsProduct CreateCropsProduct()
        {

            string ClassNamespace = AssemblyPath + ".CropsProduct";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Things.IDAL.ICropsProduct)objType;
        }


        /// <summary>
        /// ??CropsProductProgress??????
        /// </summary>
        public static Things.IDAL.ICropsProductProgress CreateCropsProductProgress()
        {

            string ClassNamespace = AssemblyPath + ".CropsProductProgress";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Things.IDAL.ICropsProductProgress)objType;
        }


        /// <summary>
        /// ??CropsProductProgressInfo??????
        /// </summary>
        public static Things.IDAL.ICropsProductProgressInfo CreateCropsProductProgressInfo()
        {

            string ClassNamespace = AssemblyPath + ".CropsProductProgressInfo";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Things.IDAL.ICropsProductProgressInfo)objType;
        }


        /// <summary>
        /// ??Factory??????
        /// </summary>
        public static Things.IDAL.IFactory CreateFactory()
        {

            string ClassNamespace = AssemblyPath + ".Factory";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Things.IDAL.IFactory)objType;
        }


        /// <summary>
        /// ??Farm??????
        /// </summary>
        public static Things.IDAL.IFarm CreateFarm()
        {

            string ClassNamespace = AssemblyPath + ".Farm";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Things.IDAL.IFarm)objType;
        }


        /// <summary>
        /// ??Farmer??????
        /// </summary>
        public static Things.IDAL.IFarmer CreateFarmer()
        {

            string ClassNamespace = AssemblyPath + ".Farmer";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Things.IDAL.IFarmer)objType;
        }


        /// <summary>
        /// 创建Goods数据层接口。
        /// </summary>
        public static Things.IDAL.IGoods CreateGoods()
        {

            string ClassNamespace = AssemblyPath + ".Goods";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Things.IDAL.IGoods)objType;
        }


        /// <summary>
        /// 创建GoodsCategory数据层接口。
        /// </summary>
        public static Things.IDAL.IGoodsCategory CreateGoodsCategory()
        {

            string ClassNamespace = AssemblyPath + ".GoodsCategory";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Things.IDAL.IGoodsCategory)objType;
        }


        /// <summary>
        /// 创建GoodsDescription数据层接口。
        /// </summary>
        public static Things.IDAL.IGoodsDescription CreateGoodsDescription()
        {

            string ClassNamespace = AssemblyPath + ".GoodsDescription";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Things.IDAL.IGoodsDescription)objType;
        }


        /// <summary>
        /// 创建GoodsPrice数据层接口。
        /// </summary>
        public static Things.IDAL.IGoodsPrice CreateGoodsPrice()
        {

            string ClassNamespace = AssemblyPath + ".GoodsPrice";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Things.IDAL.IGoodsPrice)objType;
        }


        /// <summary>
        /// ??GoodsImages??????
        /// </summary>
        public static Things.IDAL.IGoodsImages CreateGoodsImages()
        {

            string ClassNamespace = AssemblyPath + ".GoodsImages";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Things.IDAL.IGoodsImages)objType;
        }


        /// <summary>
        /// ??GoodsOrders??????
        /// </summary>
        public static Things.IDAL.IGoodsOrders CreateGoodsOrders()
        {

            string ClassNamespace = AssemblyPath + ".GoodsOrders";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Things.IDAL.IGoodsOrders)objType;
        }


        /// <summary>
        /// 创建GoodsComments数据层接口。
        /// </summary>
        public static Things.IDAL.IGoodsComments CreateGoodsComments()
        {

            string ClassNamespace = AssemblyPath + ".GoodsComments";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Things.IDAL.IGoodsComments)objType;
        }

    }
}